package com.ruoyi.fun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fun.domain.EcUserAddress;
import com.ruoyi.fun.service.IEcUserAddressService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收货地址Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/fun/address")
public class EcUserAddressController extends BaseController
{
    @Autowired
    private IEcUserAddressService ecUserAddressService;

    /**
     * 查询收货地址列表
     */
    @PreAuthorize("@ss.hasPermi('fun:address:list')")
    @GetMapping("/list")
    public TableDataInfo list(EcUserAddress ecUserAddress)
    {
        startPage();
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        // 添加组织过滤，只显示当前用户组织的收货地址
        ecUserAddress.setOrgId(orgId);
        List<EcUserAddress> list = ecUserAddressService.selectEcUserAddressList(ecUserAddress);
        return getDataTable(list);
    }

    /**
     * 导出收货地址列表
     */
    @PreAuthorize("@ss.hasPermi('fun:address:export')")
    @Log(title = "收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EcUserAddress ecUserAddress)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        ecUserAddress.setOrgId(orgId);
        List<EcUserAddress> list = ecUserAddressService.selectEcUserAddressList(ecUserAddress);
        ExcelUtil<EcUserAddress> util = new ExcelUtil<EcUserAddress>(EcUserAddress.class);
        util.exportExcel(response, list, "收货地址数据");
    }

    /**
     * 获取收货地址详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:address:query')")
    @GetMapping(value = "/{addressId}")
    public AjaxResult getInfo(@PathVariable("addressId") Long addressId)
    {
        try {
            EcUserAddress address = ecUserAddressService.selectEcUserAddressByAddressId(addressId);
            if (address == null) {
                return error("收货地址不存在");
            }
            
            // 验证地址是否属于当前用户的组织
            Long currentOrgId = getOrgId();
            // 103组织拥有不过滤的权限
            if (currentOrgId != null && currentOrgId == 103L) {
                // 允许访问
            } else if (currentOrgId != null && !currentOrgId.equals(address.getOrgId())) {
                return error("无权限访问此地址信息");
            }
            
            return success(address);
        } catch (Exception e) {
            logger.error("获取收货地址详情失败：" + e.getMessage(), e);
            return error("获取地址详情失败：" + e.getMessage());
        }
    }

    /**
     * 新增收货地址
     */
    @PreAuthorize("@ss.hasPermi('fun:address:add')")
    @Log(title = "收货地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EcUserAddress ecUserAddress)
    {
        ecUserAddress.setOrgId(getOrgId());
        return toAjax(ecUserAddressService.insertEcUserAddress(ecUserAddress));
    }

    /**
     * 修改收货地址
     */
    @PreAuthorize("@ss.hasPermi('fun:address:edit')")
    @Log(title = "收货地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EcUserAddress ecUserAddress)
    {
        return toAjax(ecUserAddressService.updateEcUserAddress(ecUserAddress));
    }

    /**
     * 删除收货地址
     */
    @PreAuthorize("@ss.hasPermi('fun:address:remove')")
    @Log(title = "收货地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{addressIds}")
    public AjaxResult remove(@PathVariable Long[] addressIds)
    {
        return toAjax(ecUserAddressService.deleteEcUserAddressByAddressIds(addressIds));
    }
    /**
     * 根据addressId和组织ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:ecuseraddress:query')")
    @GetMapping(value = "/{addressId}/org/{orgId}")
    public AjaxResult getInfoByOrgId(@PathVariable("addressId") Long addressId, @PathVariable("orgId") Long orgId)
    {
        EcUserAddress address = new EcUserAddress();
        address.setAddressId(addressId);
        address.setOrgId(orgId);
        return success(ecUserAddressService.selectEcUserAddressList(address));
    }

    /**
     * 根据组织ID查询列表
     */
    @PreAuthorize("@ss.hasPermi('fun:ecuseraddress:list')")
    @GetMapping("/org/{orgId}")
    public TableDataInfo listByOrgId(@PathVariable Long orgId)
    {
        startPage();
        EcUserAddress ecUseraddress = new EcUserAddress();
        ecUseraddress.setOrgId(orgId);
        List<EcUserAddress> list = ecUserAddressService.selectEcUserAddressList(ecUseraddress);
        return getDataTable(list);
    }


}